

cd $firmpath 
// RS draws by id_conc 
use $correspath/corres_nc819962010.dta, clear 
cap g nc8 = cn8
gegen _=count(nc8), by(id_conc year) 
sort id_conc year
by id_conc: gegen nbm=max(_)
keep if year==2002
tostring nc8, replace 
replace nc8="0"+nc8 if length(nc8)==7
g hs6_2002 =substr(nc8,1,6) if length(nc8)==8  
keep hs6 id_conc 
*destring hs6_$year, replace
merge m:1 hs6_2002 using  $rspath/tmp_rs 
keep if _m==3
collapse (median) rsbs1-rsbs400, by(id_conc) 
forvalues i=1(1)400{
 rename rsbs`i' rs`i'		
}
save ./temp2_rs, replace



clear 
g iter=0 
save $outputpath/table_bootstrap_newdeath, replace 

forvalue k=1(1)400 {
	use new WUI_ uncertainty gr_gdp new pm trim ct cty pmt year id_conc iso2 export die using base4reg_u19962010, clear
	fmerge m:1 id_conc using ./temp2_rs, keepusing(rs`k') nogen noreport keep(3)
	g eta_uncert=uncertainty*rs`k'
	g eta_WUI=WUI_*rs`k'
	g eta_gdp=gr_gdp*rs`k'


qui ppmlhdfe new uncertainty eta_uncert gr_gdp eta_gdp  if year > 1999, a(pmt cty)  tol(1.000e-06) 
foreach i in uncertainty eta_uncert gr_gdp eta_gdp {
g c_new1_`i'=_b[`i']
}

qui ppmlhdfe new eta_uncert eta_gdp if year > 1999, a(pm ct)  tol(1.000e-06) 
foreach i in eta_uncert  eta_gdp{
g c_new2_`i'=_b[`i']
}

qui ppmlhdfe new WUI_ eta_WUI gr_gdp eta_gdp if year > 1999, a(pmt cty)  tol(1.000e-06) 
foreach i in WUI_ eta_WUI gr_gdp eta_gdp{
g c_new3_`i'=_b[`i']
}

qui ppmlhdfe new eta_WUI  eta_gdp if year  > 1999, a(pm ct)  tol(1.000e-06) 
foreach i in eta_WUI eta_gdp {
g c_new4_`i'=_b[`i']
}

qui ppmlhdfe die uncertainty  eta_uncert  gr_gdp eta_gdp if year < 2007, a(pmt cty)  tol(1.000e-06) 
foreach i in uncertainty eta_uncert  gr_gdp eta_gdp {
g c_death1_`i'=_b[`i']
}

qui ppmlhdfe die uncertainty  eta_uncert eta_gdp  if year < 2007, a(pm ct)  tol(1.000e-06) 
foreach i in eta_uncert eta_gdp {
g c_death2_`i'=_b[`i']
}

qui ppmlhdfe die WUI_ eta_WUI  gr_gdp eta_gdp  if year < 2007, a(pmt cty)  tol(1.000e-06) 
foreach i in WUI_ eta_WUI gr_gdp eta_gdp {
g c_death3_`i'=_b[`i']
}

qui ppmlhdfe die eta_WUI eta_gdp  if year < 2007, a(pm ct)  tol(1.000e-06) 
foreach i in  eta_WUI  eta_gdp {
g c_death4_`i'=_b[`i']
}


keep c_death*  c_new* 
	g iter=`k' 
	keep if _n==1 
	append using $outputpath/table_bootstrap_newdeath
	save $outputpath/table_bootstrap_newdeath, replace
	dis `k'

	}
	
use $outputpath/table_bootstrap_newdeath, clear	


foreach i of varlist c_new1_uncertainty c_new1_eta_uncert c_new1_gr_gdp c_new1_eta_gdp c_new2_eta_uncert c_new2_eta_gdp c_new3_WUI_ c_new3_gr_gdp c_new3_eta_gdp c_new3_eta_WUI  c_new4_eta_WUI c_new4_eta_gdp c_death1_uncertainty c_death1_eta_uncert c_death1_gr_gdp c_death1_eta_gdp c_death2_eta_uncert c_death2_eta_gdp c_death3_WUI_ c_death3_eta_WUI c_death3_gr_gdp c_death3_eta_gdp c_death4_eta_WUI c_death4_eta_gdp {
egen sd_`i'=sd(`i')
egen m_`i'=mean(`i')
}

gcollapse (mean) m_* sd_*  
g one=1


putexcel set $outputpath/table_5.xls, replace


putexcel B1="New"
putexcel C1="New"
putexcel D1="New"
putexcel E1="New"
putexcel F1="Death"
putexcel G1="Death"
putexcel H1="Death"
putexcel I1="Death"

local j=3
foreach i in uncertainty eta_uncert gr_gdp eta_gdp {
putexcel A`j' ="`i'"
local j=`j'+2
}
	
local j=3
foreach i in uncertainty eta_uncert gr_gdp eta_gdp {
sum m_c_new1_`i'
local stat=r(mean)
putexcel B`j' =`stat' 
local j=`j'+1 
sum sd_c_new1_`i'
local stat=r(mean)
putexcel B`j' =`stat' 
local j=`j'+1 
}

local j=5
foreach i in   eta_uncert   eta_gdp {
sum m_c_new2_`i'
local stat=r(mean)
putexcel C`j' =`stat' 
local j=`j'+1 
sum sd_c_new2_`i'
local stat=r(mean)
putexcel C`j' =`stat' 
local j=`j'+3 
}

local j=3
foreach i in  WUI_ eta_WUI gr_gdp eta_gdp {
sum m_c_new3_`i'
local stat=r(mean)
putexcel D`j' =`stat' 
local j=`j'+1 
sum sd_c_new3_`i'
local stat=r(mean)
putexcel D`j' =`stat' 
local j=`j'+1 
}

local j=5
foreach i in eta_WUI eta_gdp  {
sum m_c_new4_`i'
local stat=r(mean)
putexcel E`j' =`stat' 
local j=`j'+1 
sum sd_c_new4_`i'
local stat=r(mean)
putexcel E`j' =`stat' 
local j=`j'+3 
}



local j=3
foreach i in  uncertainty eta_uncert gr_gdp eta_gdp{
sum m_c_death1_`i'
local stat=r(mean)
putexcel F`j' =`stat' 
local j=`j'+1 
sum sd_c_death1_`i'
local stat=r(mean)
putexcel F`j' =`stat' 
local j=`j'+1 
}

local j=5
foreach i in   eta_uncert   eta_gdp  {
sum m_c_death2_`i'
local stat=r(mean)
putexcel G`j' =`stat' 
local j=`j'+1 
sum sd_c_death2_`i'
local stat=r(mean)
putexcel G`j' =`stat' 
local j=`j'+3 
}

local j=3
foreach i in WUI_ eta_WUI gr_gdp eta_gdp {
sum m_c_death3_`i'
local stat=r(mean)
putexcel H`j' =`stat' 
local j=`j'+1 
sum sd_c_death3_`i'
local stat=r(mean)
putexcel H`j' =`stat' 
local j=`j'+1 
}

local j=5
foreach i in   eta_WUI   eta_gdp {
sum m_c_death4_`i'
local stat=r(mean)
putexcel I`j' =`stat' 
local j=`j'+1 
sum sd_c_death4_`i'
local stat=r(mean)
putexcel I`j' =`stat' 
local j=`j'+3 
}




/*

greshape long m_ sd_ , i(one) string
g tstat=abs(m_)/sd_
save  $outputpath/table_5, replace